//
//  DDCoverView.m
//  ddhw_ios
//
//  Created by sv on 16/12/2.
//  Copyright © 2016年 DD. All rights reserved.
//

#import "DDCoverView.h"
//#import "DDHomDetailView.h"

@interface DDCoverView()

@property (nonatomic, strong)UIButton *btn;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) BOOL keyboardIsVisible;

@end

@implementation DDCoverView


- (void)dealloc{
    QYLog(@"遮盖view--销毁");
}
- (instancetype)initWithSize:(CGSize)size subView:(UIView *)subView
{
    self= [super initWithFrame:CGRectMake(15, 0, size.width, size.height)];
    if(self){
        self.backgroundColor = [UIColor clearColor];
        subView.frame = self.bounds;
        
//        subView.frame = CGRectMake(0, 0, size.width, size.height);
        [self addSubview:subView];
        
        self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button-off"]];
        [self addSubview: self.imageView];
        [ self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(subView.mas_bottom);
            make.centerX.equalTo(self.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(38, 88));
        }];
        
    }
    return self;
}

//移动UIView
-(void)transformView:(NSNotification *)aNSNotification
{
    //获取键盘弹出前的Rect
    NSValue *keyBoardBeginBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect beginRect=[keyBoardBeginBounds CGRectValue];
    
    //获取键盘弹出后的Rect
    NSValue *keyBoardEndBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect  endRect=[keyBoardEndBounds CGRectValue];
    
    //获取键盘位置变化前后纵坐标Y的变化值
    CGFloat deltaY=beginRect.origin.y - endRect.origin.y;
    
    //在0.25s内完成self.view的Frame的变化，等于是给self.view添加一个向上移动deltaY的动画
    CGFloat moveHeight = 0.5 * self.height - (self.centerY - deltaY);
    if(moveHeight < 0)
    {
        [UIView animateWithDuration:0.25f animations:^{
            self.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT *0.5);
        }];
    }else{
        [UIView animateWithDuration:0.25f animations:^{
            self.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT *0.5 - moveHeight);
        }];
    }
}

- (void)setIsShowCancelImage:(BOOL)isShowCancelImage
{
    self.imageView.hidden = !isShowCancelImage;
}

#pragma mark-主要方法
- (void)showInView:(UIView *)view
{
    [view addSubview:self];
    self.btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [UIScreen mainScreen].bounds.size.height)];
    [self.btn setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
    [view insertSubview:self.btn belowSubview:self];
    [self.btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.center = CGPointMake(SCREEN_WIDTH/2, 720);
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT *0.5);
    }];
}
- (void)buttonClick
{
    if(self.isNoClick) return;
    [self hidden];
}

- (void)hidden
{

    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.center = CGPointMake(SCREEN_WIDTH/2, 720 * 1.5);
    } completion:^(BOOL finished) {
        
        [weakSelf.btn removeFromSuperview];
        [weakSelf removeFromSuperview];
    }];
}


@end
